package com.ms.oschina.ui.activity;

import java.io.IOException;

import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.ms.oschina.R;
import com.ms.oschina.adapter.TweetCommentAdapter;
import com.ms.oschina.api.remote.OkHttpClientManager.OnGetNetResultListener;
import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseDetailsActivity;
import com.ms.oschina.base.LoadingPager.ResultCode;
import com.ms.oschina.bean.CommentList;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.HttpResponse;
import com.ms.oschina.bean.Tweet;
import com.ms.oschina.bean.TweetDetail;
import com.ms.oschina.fragment.NewsDetailBottomEmojiFragment;
import com.ms.oschina.fragment.NewsDetailBottomEmojiFragment.OnCreateListener;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.TweetDetailHeadViewHolder;

public class TweetsDetailsActivity extends BaseDetailsActivity {
	private NewsDetailBottomEmojiFragment detailBottomFragment;
	private TweetDetail tweetDetail;

	private ListView lv;
	private SwipeRefreshLayout refresh;
	private Tweet tweet;

	private void findViews(View view) {
		refresh = (SwipeRefreshLayout) view.findViewById(R.id.srl_refresh);
		lv = (ListView) view.findViewById(R.id.lv);
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.actionbar_title_tweet);
	}

	@Override
	protected Fragment bottomControlFragement() {
		detailBottomFragment = (NewsDetailBottomEmojiFragment) Fragment
				.instantiate(this,
						NewsDetailBottomEmojiFragment.class.getName());
		detailBottomFragment.setOnCreateListener(new OnCreateListener() {
			public void OnCreate() {
				detailBottomFragment.openCommentPanel(false);
				detailBottomFragment.setHindConvertView();
			}
		});
		return detailBottomFragment;
	}

	@Override
	protected View loadSuccessView() {
		View view = UiUtils.inflate(R.layout.pulltorefresh);
		findViews(view);
		setData();
		setEvent();
		return view;
	}

	private void setEvent() {

	}

	private void setData() {
		refresh.setColorSchemeResources(R.color.swiperefresh_color1,
				R.color.swiperefresh_color2, R.color.swiperefresh_color3,
				R.color.swiperefresh_color4);
		MySwipeRefreshListener listener = new MySwipeRefreshListener();
		refresh.setOnRefreshListener(listener);

		// 给listview 添加头布局
		TweetDetailHeadViewHolder vh = new TweetDetailHeadViewHolder();
		lv.addHeaderView(vh.getRootView());
		tweet = tweetDetail.getTweet();
		vh.setData(0, tweet);
		int id = tweet.getId();
		getNetComment(id);
	}

	private void getNetComment(final int id) {
		OsChinaApi.getAsyncTweetComment(id, 0, 0,
				new OnGetNetResultListener<CommentList>() {
					private TweetCommentAdapter adapter;

					@Override
					public void onGetNetResult(int requestCode,
							HttpResponse<CommentList> result) {
						refresh.setRefreshing(false);
						if (result.e != null) {// 网络错误
							UiUtils.showToast("网络错误");
						} else {
							CommentList response = result.response;

							if (response != null) {
								if (adapter == null) {
									adapter = new TweetCommentAdapter(id, lv,
											response.getCommentlist(), true);
									lv.setAdapter(adapter);
								} else {
									adapter.setListData(response
											.getCommentlist());
								}
							}
						}
					}
				});
	}

	public class MySwipeRefreshListener implements
			SwipeRefreshLayout.OnRefreshListener {
		@Override
		public void onRefresh() {
			int id = tweet.getId();
			getNetComment(id);
		}
	}

	@Override
	protected ResultCode initNetData() {
		int tweetsDetailsId = getIntent().getIntExtra(
				Constants.TWEETSDETAILSID, -1);
		try {
			tweetDetail = OsChinaApi.getTweetDetail(tweetsDetailsId);
		} catch (IOException e) {
			e.printStackTrace();
			return ResultCode.STATUS_ERRORS;
		}
		return ResultCode.STATUS_SUCCESS;
	}
}
